package cn.zhangxh.org.service.impl;

import cn.zhangxh.base.service.impl.BaseServiceImpl;
import cn.zhangxh.org.domain.Department;
import cn.zhangxh.org.mapper.DepartmentMapper;
import cn.zhangxh.org.query.DepartmentQuery;
import cn.zhangxh.org.service.IDepartmentService;
import cn.zhangxh.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @param :
 * @Author zxh
 * @Date: 2022/1/18$ 14:28$
 * @Version 1.0
 * @return :
 * @Description: DepartmentServiceImpl$
 */
@Service
/*事务配置,默认是readOnly = false*/
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        //前段传来department数据的时候 需要保存dirPath
        //声明需要保存的dirPath
        String dirPath = "";
        //1.新增的是顶级部门 : /+自己的id
        if (department.getParent() == null){
            dirPath = "/" + department.getId();
        }else {
            //2.新增的不是顶级部门
            //根据父亲的id,查出父亲这个对象
            Department parentDept = departmentMapper.queryById(department.getParent().getId());
            //父亲的dirPath + 自己的
            dirPath = parentDept.getDirPath() + "/" + department.getId();
        }
        //设置dirPath,然后更新
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    /**
     * @Date: 2022/1/22 23:14
     * @return :
     * @param : 前端拿来的所有的id
     * @Description : 批量阐述
     */
    @Override
    public void batchRemove(List<Long> id) {
        departmentMapper.batchRemove(id);
    }

    /**
     * @Date: 2022/1/24 11:38
     * @return :
     * @param :
     * @Description : 查找部门树
     */
    @Override
    public List<Department> findTree() {
        return departmentMapper.findTree();
    }
}
